package com.atu.poker.service;

import javax.mail.MessagingException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.atu.poker.dao.UserInfoDAO;
import com.atu.poker.dao.impl.UserInfoDAOJdbcImpl;
import com.atu.poker.po.UserInfo;
import com.atu.poker.util.DateTimeUtil;
import com.atu.poker.util.MailUtil;
import com.atu.poker.util.MapUtil;
import com.atu.poker.util.RespUtil;
import com.atu.poker.util.UserUtil;

public class UserInfoService {

	private static Logger logger = LogManager.getLogger(UserInfoService.class);
	private UserInfoDAO userInfoDAO = new UserInfoDAOJdbcImpl();
	public static MapUtil<String, Integer> emailCodeMap = new MapUtil<>();

	/**
	 * 验证码验证成功则注册用户成功
	 * 
	 * @param userName
	 * @param email
	 * @param emailCode
	 * @param userPwd
	 * @return
	 */
	public Object addUserInfo(String userName, String email, String emailCode, String userPwd) {
		if (emailCodeMap.map.get(email).toString().equals(emailCode)) {
			// 验证码验证成功则从map中删除邮箱和验证码信息，如果注册用户异常，则需要再次发送验证码进行验证
			emailCodeMap.map.remove(email);

			String userId = UserUtil.getUUID();

			UserInfo userinfo = new UserInfo();
			userinfo.setUserId(userId);
			userinfo.setUserName(userName);
			userinfo.setEmail(email);
			userinfo.setVip("0");
			userinfo.setRoomKey("0");
			userinfo.setUserChip("0");
			userinfo.setUserStatus("0");
			userinfo.setUserPwd(userPwd);
			userinfo.setCreateTime(DateTimeUtil.getCurrentDateTime());
			userInfoDAO.addUserInfo(userinfo);
			// 注册成功201
			return RespUtil.respTypeCode("201");
		} else {
			// 验证码错误202
			return RespUtil.respTypeCode("202");
		}

	}

	/**
	 * 根据邮箱和密码判断用户登录
	 * 
	 * @param email
	 * @param userPwd
	 * @return
	 */
	public Object loginUser(String email, String userPwd) {
		String userId = userInfoDAO.getIdByEmail(email) == null ? "" : userInfoDAO.getIdByEmail(email);
		if ("".equals(userId)) {
			// 邮箱不存在301
			return RespUtil.respTypeCode("301");
		} else {
			if (UserUtil.decrypt(userPwd).equals(UserUtil.decrypt(userInfoDAO.getUserPwd(email)))) {
				if ("0".equals(userInfoDAO.getUserStatus(userId))) {
					UserInfo userInfo = null;
					userInfoDAO.changeUserStatus(userId, "1");
					userInfo = userInfoDAO.getUserInfo(userId);
					// 登录成功305，发送用户信息
					return RespUtil.respTypeMessage("305", "", "userInfo", userInfo);
				} else {
					// 用户已登录302
					return RespUtil.respTypeCode("302");
				}
			} else {
				// 密码错误303
				return RespUtil.respTypeCode("303");
			}
		}
	}

	/**
	 * 验证码验证成功则修改用户名密码
	 * 
	 * @param email
	 * @param emailCode
	 * @param userPwd
	 * @return
	 */
	public Object upUserInfo(String email, String emailCode, String userPwd) {
		if (emailCodeMap.map.get(email).toString().equals(emailCode)) {
			userInfoDAO.upUserPwd(email, userPwd);
			// 失败则不删除该邮箱验证码
			emailCodeMap.map.remove(email);
			// 成功修改密码401
			return RespUtil.respTypeCode("401");
		} else {
			// 验证码错误402
			return RespUtil.respTypeCode("402");
		}
	}

	/**
	 * 注册用户时发送验证码，发送成功则保存邮箱和验证码
	 * 
	 * @param email
	 * @return
	 */
	public Object addConfirmEmail(String userName, String email) {
		try {
			if (confirmUserName(userName)) {
				String userId = userInfoDAO.getIdByEmail(email) == null ? "" : userInfoDAO.getIdByEmail(email);
				if ("".equals(userId)) {
					String title = "欢迎注册二公一";
					int emailCode = (int) ((Math.random() * 9 + 1) * 100000);

					MailUtil.sendMail(title, email, "请确认验证码【" + emailCode + "】进行注册");
					logger.debug("注册用户验证码"+emailCode);
					emailCodeMap.map.put(email, emailCode);
					// 验证码已发送至邮箱101
					return RespUtil.respTypeCode("101");

				} else {
					// 邮箱已被占用102
					return RespUtil.respTypeCode("102");
				}

			} else {
				// 用户已被占用103
				return RespUtil.respTypeCode("103");
			}
		} catch (MessagingException e) {
			// 发送验证码异常104
			return RespUtil.respTypeCode("104");
		}
	}

	/**
	 * 修改密码时发送验证码，发送成功则保存邮箱和验证码
	 * 
	 * @param email
	 * @return
	 */
	public Object upConfirmEmail(String email) {
		try {
			String userId = userInfoDAO.getIdByEmail(email) == null ? "" : userInfoDAO.getIdByEmail(email);
			if (!"".equals(userId)) {
				String title = "您正在修改二公一密码";
				int emailCode = (int) ((Math.random() * 9 + 1) * 100000);

				MailUtil.sendMail(title, email, "请确认验证码【" + emailCode + "】进行修改");
				logger.debug("修改密码验证码"+emailCode);
				// 保存邮箱和验证码信息，如果发送邮箱异常，则不保存
				emailCodeMap.map.put(email, emailCode);
				// 验证码已发送至邮箱501
				return RespUtil.respTypeCode("501");

			} else {
				// 邮箱不存在502
				return RespUtil.respTypeCode("502");
			}
		} catch (MessagingException e) {
			// 验证码发送异常503
			return RespUtil.respTypeCode("503");
		}
	}

	/**
	 * 判断用户名是否已经被占用,返回json由addConfirmEmail返回
	 * 
	 * @param userName
	 * @return
	 */
	public boolean confirmUserName(String userName) {
		String userId = userInfoDAO.getIdByUserName(userName) == null ? "" : userInfoDAO.getIdByUserName(userName);
		if ("".equals(userId)) {
			// 用户名没有被占用
			return true;
		} else {
			// 已被占用
			return false;
		}
	}

	public void changeLoginStatus(String userId, String userStatus) {
		userInfoDAO.changeUserStatus(userId, userStatus);
	}
}
